import {
    AsyncStorage
} from 'react-native'
import GitHubTrending from "GitHubTrending";

export let FLAG_STORAGE = {flag_popular: 'popular', flag_trending: 'trending'}
export default class DataRepository {
    constructor(flag) {
        this.flag = flag
        if(this.flag === FLAG_STORAGE.flag_trending) this.trending = new GitHubTrending()
    }
    fetchRepository(url) {
        return new Promise((resolve, reject) => {
            // 获取本地数据
           this.fetchLocalRepository(url)
               .then(result => {
                   if(result) {
                       resolve(result)
                   } else {
                       this.fetchNetRepository(url)
                           .then(res=> {
                               resolve(res)
                           })
                           .catch(e=>{
                               reject(e)
                           })
                   }
               })
               .catch(e=>{
                   reject(e)
               })
        })
    }
    fetchLocalRepository(url) {
        return new Promise((resolve, reject) => {
            // 获取本地数据
            AsyncStorage.getItem(url, (error, result) => {
                if(!error) {
                    try{
                        resolve(JSON.parse(result))
                    }catch (e) {
                        alert(e)
                        reject(e)
                    }
                }
            })
        })
    }
    fetchNetRepository(url) {
        return new Promise((resolve, reject) => {
            if(this.flag === FLAG_STORAGE.flag_trending) {
                this.trending.fetchTrending(url)
                    .then(result => {
                        if(!result) {
                            reject(new Error('获取数据为空'))
                            return
                        }
                        this.saveRepository(url, result)
                        resolve(result)
                    })
            } else {
                fetch(url)
                    .then(res => res.json())
                    .then(res => {
                        if(!res) {
                            reject(new Error('获取数据为空'))
                            return
                        }
                        resolve(res.items);
                        this.saveRepository(url, res.items)
                    })
                    .catch(error => {
                        reject(error)
                    })
            }

        })
    }
    saveRepository(url, items, callback) {
        if(!url || !items) return;
        let wrapData = {items:items, update_date: new Date().getTime()}
        AsyncStorage.setItem(url, JSON.stringify(wrapData), callback)
    }
    checkDate(longTime) {
        let curDate = new Date();
        let tDate = new Date()
        tDate.setTime(longTime)
        if(curDate.getMonth() !== tDate.getMonth()) return false;
        if(curDate.getDay() !== tDate.getDay()) return false;
        if(curDate.getHours() - tDate.getHours() > 4) return false;
        return true;

    }
}